package com.elcom.roaming.common.config;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.slf4j.Logger;

public class ConfigLoader extends ConfigInit{

	private Logger logger;
	
	public ConfigLoader(Logger logger, String configFolder, String configFile) {
		super(configFolder, configFile);
		this.logger = logger;
	}

	@Override
	protected void createDefaultConfigFile() {
		BufferedWriter w;
		try {
			w = new BufferedWriter(new FileWriter(super.configFile));
			w.write("<?xml version=\"1.0\" ?>");
			w.newLine();
			w.write("\t<properties>");
			w.newLine();
			
			w.write("\t\t<category name=\"ORACLE\">");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_DRIVER\" value=\"oracle.jdbc.driver.OracleDriver\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_CONNECT_STRING\" value=\"jdbc:oracle:thin:@(description=(address=(host=192.168.6.112)(protocol=tcp)(port=1521))(connect_data=(sid=pm3fs)))\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_USERNAME\" value=\"chautn\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_PASSWORD\" value=\"chautn\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_MAX_CONNECTION\" value=\"5\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_QUERY_BATCH\" value=\"500\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_MAX_QUEUE_SIZE\" value=\"100000\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"DB_INSERT_TIME\" value=\"5\"/>");
			w.newLine();
			if (ConfigModule.MODULE_NAME.equals(ConfigModule.SYNTHESIS)) {
				w.write("\t\t\t<property name=\"DB_UPDATE_CACHE_TIME\" value=\"5\"/>");
				w.newLine();
			}
			w.write("\t\t</category>");
			w.newLine();

			w.write("\t\t<category name=\"MATCHING\">");
			w.newLine();
			w.write("\t\t\t<property name=\"MATCHING_CLEANING_MAP_TIME\" value=\"120\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"MATCHING_MAX_QUEUE_SIZE\" value=\"100000\"/>");
			w.newLine();
			w.write("\t\t\t<property name=\"MATCHING_MAX_MAP_SIZE\" value=\"100000\"/>");
			w.newLine();
			w.write("\t\t</category>");
			w.newLine();

			w.write("\t\t<category name=\"MODULE\">");
			w.newLine();
			w.write("\t\t\t<property name=\"MISS_CALL_TIMEOUT\" value=\"90\"/>");
			w.newLine();
			w.write("\t\t</category>");
			w.newLine();

			if (ConfigModule.MODULE_NAME.equals(ConfigModule.SYNTHESIS)) {
				w.write("\t\t<category name=\"SYNTHESIS_MATCHING\">");
				w.newLine();
				w.write("\t\t\t<property name=\"MATCHING_MAX_MSRN_MAP_SIZE\" value=\"100000\"/>");
				w.newLine();
				w.write("\t\t</category>");
				w.newLine();

				w.write("\t\t<category name=\"SYNTHESIS_LB\">");
				w.newLine();
				w.write("\t\t\t<property name=\"LB_HOST\" value=\"192.168.6.248\"/>");
				w.newLine();
				w.write("\t\t\t<property name=\"LB_PORT\" value=\"7201\"/>");
				w.newLine();
				w.write("\t\t\t<property name=\"LB_CLIENT_MAX_QUEUE_SIZE\" value=\"100000\"/>");
				w.newLine();
				w.write("\t\t</category>");
				w.newLine();

			}
			
			w.write("\t</properties>");
			w.flush();
			w.close();
		} catch (IOException ex) {
			logger.error("Can not create the default config file.", ex);
		}
	}

	@Override
	protected void onError(Exception e) {
		logger.error("Load config fail.", e);
	}

}
